using System;
using System.Web;

using SBPweb.Forge.Configuration;

namespace SBPweb.Forge.Web
{
	/// <summary>
	/// Summary description for CookieHandler
	/// </summary>
	public class CookieHandler
	{

		private static HttpCookieCollection cookies
		{
			get
			{
				if (HttpContext.Current == null || HttpContext.Current.Request == null)
					return null;
				return HttpContext.Current.Request.Cookies;
			}
		}

		private const string KEY_DEFAULT_CULTURE = "DefaultCulture";
		/// <summary>
		/// Default culture
		/// </summary>
		public static string DefaultCulture
		{
            get
            {
                return Get(KEY_DEFAULT_CULTURE, "en-US");
            }
            set
            {
                Put(KEY_DEFAULT_CULTURE, value);
            }
        }

		private const string KEY_DEFAULT_THEME = "DefaultTheme";
		/// <summary>
		/// Default theme
		/// </summary>
		public static string DefaultTheme
		{
			get
			{
                return Get(KEY_DEFAULT_THEME, "Default");
			}
			set
			{
                Put(KEY_DEFAULT_THEME, value);
            }
		}

        public static string Get(string name)
        {
            return Get(name, string.Empty);
        }

        public static string Get(string name, string defaultValue)
        {
            string key = string.Format("{0}.{1}", ConfigurationHandler.ApplicationTitle, name);
            if (cookies == null || cookies[key] == null)
            {
                return defaultValue;
            }
            return cookies[key].Value as string;
        }

        public static void Put(string name, string value)
        {
            HttpCookie cookie = new HttpCookie(string.Format("{0}.{1}", ConfigurationHandler.ApplicationTitle, name), value);
            cookie.Expires = DateTime.MaxValue;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

	}

}